<template>
  <div class="container">
    <div class="top">
      <div class="search">
        <Head :show="show" />
      </div>
      <div v-show="!show">
        <!-- <div class="nav" v-show="!show" >
          <div class="navItem" @click="showactive" >综合</div>
          <div class="navItem" @click="showclickprice">价格</div>
          <div class="navItem" @click="showactive">分类</div>
        </div> -->
        <div class="nav" v-show="!show">
          <div
            v-for="(box, index) in boxes"
            :key="index"
            :class="{active1:box.isActive,navItem:!box.isActive }"
            @click="handleClick(index)"
            >
            {{box.label}}
          </div>
        </div>
        <div v-show="clickprice" class="clickprice">
          <div class="pricetop">
            <div class="firstthow">
              <div class="span">筛选</div>
              <div class="firstthowItem">
                <input
                  class="low"
                  placeholder="最低价"
                  v-model.lazy="floorPrice"
                />
                <div class="gap">一</div>
                <input
                  class="high"
                  placeholder="最高价"
                  v-model.lazy="upperPrice"
                />
              </div>
            </div>
            <div class="secendItem">
              <div class="span">排序</div>
              <div class="secendthowItem">
                <div
                  :class="{ active: isActive1, low1: !isActive1 }"
                  @click="tosort(1)"
                >
                  从低到高
                </div>
                <div
                  :class="{ active: isActive2, low1: !isActive2 }"
                  @click="tosort(2)"
                >
                  从高到低
                </div>
              </div>
            </div>
          </div>
          <div class="bottom">
            <div class="cancle" @click="cancle">取消</div>
            <div class="confirm" @click="confirm">确定</div>
          </div>
        </div>
      </div>
    </div>
    <div v-show="showindexbottom">
      <div class="content" v-show="goodesdata">
        <div class="goodsList">
          <ul class="goods">
            <li class="goodsItem" @click="toDetail" v-for="item in goodesdata" :key="item.id">
              <!-- 商品图片 -->
              <img class="goodsImg" :src="item.listPicUrl" alt="" />
              <!-- 商品名称 -->
              <div class="goodsName">
                <img
                  class="nameImg"
                  :src="item.preLogo[0]?.logoUrl"
                  alt=""
                  v-show="item.preLogo[0]?.logoUrl"
                />
                <span class="nameText">{{ item.name }}</span>
              </div>
              <!-- 商品价格 -->
              <div
                class="goodsPrice"
                v-show="item.finalPriceInfoVO?.priceInfo?.finalPrice?.price"
              >
                <span class="PriceImg">￥</span>
                <span class="Price"
                  >{{ item.finalPriceInfoVO?.priceInfo?.finalPrice?.price }}
                </span>
                <div
                  class="PriceOutAll"
                  v-show="
                    item.finalPriceInfoVO?.priceInfo?.finalPrice?.price <
                    item.counterPrice
                  "
                >
                  <span class="PriceOut">￥</span>
                  <span class="PriceOut">{{ item.counterPrice }}</span>
                </div>
              </div>
              <!-- 特价 -->
              <div class="SaleList">
                <!-- <div class="SaleText">满99减30</div> -->
                <div
                  class="SaleItem"
                  v-show="
                    item.finalPriceInfoVO?.banner?.title ||
                    item.finalPriceInfoVO?.banner?.content
                  "
                >
                  <span
                    class="SaleName"
                    v-show="item.finalPriceInfoVO?.banner?.title"
                    >{{ item.finalPriceInfoVO?.banner?.title }}</span
                  >
                  <span class="SaleC">{{
                    item.finalPriceInfoVO?.banner?.content
                  }}</span>
                </div>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <div v-show="!goodesdata">
        <div>
          <img
            src="../../../assets/search.png"
            alt=""
            style="width: 100%; height: 400px"
          />
        </div>
      </div>
    </div>
    <div class="overlay" v-show="clickprice" @click="showclickprice"></div>
  </div>
</template>
<script lang="ts" setup>
  import Head from "../component/head.vue";
  import { ref, onMounted } from "vue";
  import { useRoute, useRouter } from "vue-router";
  import { storeToRefs } from "pinia";
  import { useSearchStore } from "../../../store/searchHead";
  import { getdata } from "../../../api/search";
  const searchStore = useSearchStore();
  const { goodesdata,show,showindexbottom } = storeToRefs<any>(searchStore);
  const route = useRoute();
  const router = useRouter();
  const keyword = ref(route.query.keyword as string);
  let time = Date.now();
  const timestamp = ref<number>(time);
  const _stat_search = ref<string>("autoComplete");
  //控制蒙版层的显示和隐藏
   const showclickprice=()=>{
    clickprice.value = !clickprice.value;
   }
  //控制价格的搜索的功能
  const clickprice = ref<boolean>(false);
  // console.log(timestamp);
  //con控制价格   价格排序
  const floorPrice = ref<number>();
  const upperPrice = ref<number>();
  //价格从高到低的排序
  const descSorted = ref<Boolean>(false);
  const isActive1 = ref<boolean>(false);
  const isActive2 = ref<boolean>(false);
  //三个可以点击的盒子
  const boxes=ref<any>([
      { label: '综合', isActive: false },
      { label: '价格', isActive: false },
      { label: '分类', isActive: false }
    ]);
  //从高到底,从低到高
  const tosort = (index: number) => {
    if (index === 1) {
      //从低到高
      descSorted.value = false;
      // console.log(descSorted2.value);
      isActive1.value = !isActive1.value;
      isActive2.value = false; // 取消另一个元素的高亮状态
    } else if (index === 2) {
      descSorted.value = true;
      isActive2.value = !isActive2.value;
      isActive1.value = false; // 取消另一个元素的高亮状态
    }
  };
  //点击取消
  const cancle = () => {
    clickprice.value = false;
    descSorted.value = false;
    floorPrice.value=undefined;
    upperPrice.value=undefined;
    descSorted.value = false;
  };
  //点击确定
  const confirm = () => {
    const getGoodes = async () => {
      if(floorPrice.value&&upperPrice.value&&floorPrice.value!=-1&&upperPrice.value!=-1){
        floorPrice.value=floorPrice.value;
        upperPrice.value=upperPrice.value;
      }else{
        floorPrice.value=-1;
        upperPrice.value=-1;
      }
      const res = await getdata(
        timestamp.value,
        keyword.value,
        floorPrice.value,
        upperPrice.value,
        descSorted.value,
        _stat_search.value
      );
      // console.log(res);
      goodesdata.value = res.data.directlyList;
    };
    getGoodes();
    clickprice.value = false;
    floorPrice.value=undefined;
    upperPrice.value=undefined;
  };
  //获取商品
  const getGoodes = async () => {
  const res = await getdata(
    timestamp.value,
    keyword.value,
    -1,
    -1,
    descSorted.value,
    _stat_search.value
  );
  goodesdata.value = res.data.directlyList;
  };
  //点击盒子,点谁谁高亮
  const handleClick = (index:number) => {
      // 点击盒子时，将其设为高亮状态，其他盒子取消高亮
      boxes.value.forEach((box:any, i:number) => {
        // console.log(box,i);
        box.isActive = i === index;
      });
      //点击价格时,控制排序等功能框的显示与隐藏
      if (index === 1) {
        clickprice.value = !clickprice.value;
      }
      if (index === 0||index === 2) {
        clickprice.value = false;
      }

    };

    /* 跳转到详情页 */
    const toDetail = () => {
      router.push('/detail')
    }


  //挂载执行的函数
  onMounted(() => {
    getGoodes();
    floorPrice.value=undefined;
    upperPrice.value=undefined;
  });
</script>
<script lang="ts">
export default {
  name: "",
};
</script>
<style lang="scss" scoped>
.active {
  border: 2px solid #ff6700;
  width: 100px;
  height: 28px;
  box-sizing: border-box;
  line-height: 28px;
  text-align: center;
  margin-right: 10px;
}
//价格的三子盒子的高亮样式
.active1{
  width: 33%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-size: 14px;
  box-sizing: border-box;
  color: #eb1212;
}
.container {
  position: relative;
  height: 100%;
  .top {
    width: 100%;
    z-index: 100;
    position: fixed;
    top: 0;
    left: 0;
    .search {
      width: 100%;
    }
    .nav {
      width: 100%;
      display: flex;
      justify-content: space-around;
      height: 40px;
      background-color: #f5f5f5;
      border-bottom: 1px solid #aaa9a9;
      position: fixed;
      overflow: hidden;
      .navItem {
        width: 33%;
        height: 40px;
        line-height: 40px;
        text-align: center;
        font-size: 14px;
        color: #666;
        /* border: 1px solid #666; */
      }
    }
    .clickprice {
      width: 375px;
      height: 168px;
      position: absolute;
      top: 2.54rem;
      z-index: 100;
      background-color: #fff;
      font-size: 14px;
      .pricetop {
        box-sizing: border-box;
        height: 116px;
        border-bottom: 1px solid #aaa9a9;
        display: flex;
        flex-direction: column;
        padding: 10px 15px;
        justify-content: space-around;
        .firstthow {
          display: flex;
          .span {
            margin: 0 10px;
            width: 40px;
            height: 28px;
            line-height: 28px;
            text-align: center;
          }
          .firstthowItem {
            display: flex;
            .low,
            .high {
              &:focus {
                border: 2px solid red;
              }
              width: 110px;
              height: 28px;
              border: 1px solid #1b1b1b;
              line-height: 28px;
              text-align: center;
              color: #9d9d9d;
            }
            .gap {
              margin: 0 10px;
              line-height: 28px;
            }
          }
        }
        .secendItem {
          display: flex;
          .span {
            margin: 0 10px;
            width: 40px;
            height: 28px;
            line-height: 28px;
            text-align: center;
          }
          .secendthowItem {
            display: flex;

            .low1,
            .low2 {
              width: 100px;
              height: 28px;
              box-sizing: border-box;
              border: 1px solid #1b1b1b;
              line-height: 28px;
              text-align: center;
              margin-right: 10px;
            }
          }
        }
      }
      .bottom {
        display: flex;
        div {
          width: 50%;
          height: 53px;
          text-align: center;
          line-height: 53px;
          &:nth-child(1) {
            border-right: 1px solid #aaa9a9;
          }
          &:nth-child(2) {
            color: red;
          }
          // line-height:
        }
      }
    }
  }
  .content {
    position: absolute;
    top: 100px;
    left: 0;
    .goodsList {
      padding: 10px;
      .goods {
        display: flex;
        // width: 340px;
        justify-content: space-between;
        flex-wrap: wrap;
        .goodsItem {
          margin-bottom: 10px;
          height: 302px;
          width: 50%;
          display: flex;
          flex-direction: column;
          justify-content: space-around;
          .goodsImg {
            width: 173px;
            height: 173px;
            border-radius: 10px;
          }
          .goodsName {
            width: 165px;
            margin-top: 8px;
            .nameImg {
              width: 18px;
              height: 18px;
            }
            .nameText {
              font-size: 0.36rem;
              margin-left: 5px;
            }
          }
          .goodsPrice {
            display: flex;
            height: 38px;
            align-items: center;
            margin-top: 10px;
            .PriceImg {
              width: 9px;
              height: 17px;
              color: red;
              font-size: 10px;
              font-weight: 600;
            }
            .Price {
              height: 28px;
              color: red;
              font-size: 18px;
              font-weight: 600;
            }
            .PriceOutAll {
              display: flex;
              align-items: center;
              margin-left: 6px;
            }
            .PriceOut {
              text-decoration: line-through;
              height: 17px;
              color: #aaa;
              font-size: 10px;
            }
          }
        }
        .SaleList {
          width: 80px;
          height: 30px;
          .SaleText {
            background-color: red;
            color: #fff;
            border-radius: 20px;
            text-align: center;
            padding: 2px;
            font-size: 12px;
          }
          .SaleItem {
            width: 160px;
            height: 20px;
            border-radius: 20px;
            background-color: #ffe;
            text-align: center;
            align-items: center;
            display: flex;
            .SaleName {
              background-color: red;
              color: #fff;
              border-radius: 20px;
              font-size: 10px;
              padding: 2px;
            }
            .SaleC {
              display: block;
              overflow: hidden;
              white-space: nowrap;
              text-overflow: ellipsis;
              color: red;
              font-size: 11px;
              font-weight: 600;
              margin-left: 5px;
            }
          }
        }
      }
    }
  }
  .overlay {
    z-index: 99;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.54); /* 设置遮罩层的颜色和透明度 */
  }
}
</style>
